# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 10:59:02 2019

@author: leslielee
"""

from BP import BP
from read_mnist import read_train
import numpy as np
import datetime as dt

#创建神经网络实例
input_nodes = 784
hidden_nodes = 500
output_nodes = 10
learning_rate = 0.5
ANN = BP(input_nodes,hidden_nodes,output_nodes,learning_rate)

#初始化权重
W1 = np.random.normal(0.0,pow(hidden_nodes,-0.5),(hidden_nodes,input_nodes))
W2 = np.random.normal(0.0,pow(output_nodes,-0.5),(output_nodes,hidden_nodes))  
 

#读取数据
train_images,train_labels = read_train()
print(train_images[1].shape)
print(train_labels[1].shape)

#训练神经网络
for i in range(60000):
    ANN.W(W1,W2) #更新权重
    W1,W2,hidden_error,output_error = ANN.train(train_images[i],train_labels[i])
    print('======================')
    print(output_error)

#保存文件的时间
time_string1 = dt.datetime.now().strftime('%F ')
time_string2= dt.datetime.now().strftime('%T')
time_string=time_string1+time_string2[-8:-6]+'_'+time_string2[-5:-3]+'_'+time_string2[-2:]

#保存训练好的权重
np.save(time_string+'_lr'+str(learning_rate)+'_hidden_nodes'+str(hidden_nodes)+"_W1.npy",W1)
np.save(time_string+'_lr'+str(learning_rate)+'_hidden_nodes'+str(hidden_nodes)+'_W2.npy',W2)

